package com.cheyunkeji.er.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;

import androidx.core.content.ContextCompat;


public class TopBar extends LinearLayout implements View.OnClickListener {
    public LinearLayout vTopBarItem;
    public TextView vLeft;
    public TextView vRight;
    public TextView vTitle;
    Context context;
    LayoutInflater mInflater;
    View mTopBar;
    public TextView vShare;

    public TopBar(Context context) {
        super(context);
        this.context = context;
        init(context);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);

        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            switch (typedArray.getIndex(i)) {
                case R.styleable.TopBar_tb_bkg_color:
                    vTopBarItem.setBackgroundColor(typedArray.getColor(typedArray.getIndex(i), Color.parseColor("#00000000")));
                default:
                    break;
            }
        }
        typedArray.recycle();
    }

    public TextView getRightView() {
        return vRight;
    }

    public void init(Context context) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mTopBar = mInflater.inflate(R.layout.er_v_topbar, null);
        addView(mTopBar);

        vTopBarItem = (LinearLayout) findViewById(R.id.vTopBarItem);
        vLeft = (TextView) findViewById(R.id.vLeft);
        vRight = (TextView) findViewById(R.id.vRight);
        vTitle = (TextView) findViewById(R.id.vTitle);
        vShare = (TextView) findViewById(R.id.vshare);
    }

    public void setBackgroundColor(int colorId) {
        mTopBar.setBackgroundColor(colorId);
    }

    public void setTitle(String text) {
        vTitle.setText(text);
    }

    //
    public void setLeft(String text, int img, OnClickListener onClickListener) {
        vLeft.setText(text);
        if (img > 0) {
            vLeft.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context, img), null, null, null);
            vLeft.setCompoundDrawablePadding((int) context.getResources().getDimension(R.dimen.padding_few));
        }
        vLeft.setOnClickListener(onClickListener);
    }

    public void setRight(String text, int img, OnClickListener onClickListener) {
        vRight.setText(text);
        if (img > 0) {
            vRight.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context, img), null);
            vRight.setCompoundDrawablePadding((int) context.getResources().getDimension(R.dimen.padding_few));
        }
        vRight.setOnClickListener(onClickListener);
    }

    public void setRight(String text, int img,int marginRight, OnClickListener onClickListener) {
        vRight.setText(text);
        if (img > 0) {
            vRight.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context, img), null);
            vRight.setCompoundDrawablePadding((int) context.getResources().getDimension(R.dimen.padding_few));
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        layoutParams.rightMargin = marginRight;
        vRight.setLayoutParams(layoutParams);
        vRight.setGravity(Gravity.CENTER);
        vRight.setOnClickListener(onClickListener);
    }

    //
    public void setLeftBack() {
        setLeft("", R.mipmap.er_ic_back, new OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.getInstance().removeActivity(context.getClass());
                ((Activity)context).finish();
            }
        });
    }

    @Override
    public void onClick(View view) {

    }

    public void hideRight() {

        vRight.setVisibility(View.GONE);
    }
}
